package cn.tedu._03_vehicle.controller;

import cn.tedu._03_vehicle.base.response.JsonResult;
import cn.tedu._03_vehicle.base.response.StatusCode;
import cn.tedu._03_vehicle.mapper.UserMapper;
import cn.tedu._03_vehicle.mapper.VehicleMapper;
import cn.tedu._03_vehicle.pojo.dto.UserListQuery;
import cn.tedu._03_vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03_vehicle.pojo.entity.Vehicle;
import cn.tedu._03_vehicle.pojo.vo.UserRegQuery;
import cn.tedu._03_vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Validated
@RestController
@RequestMapping("/v1/vehicle/")
@Slf4j
@Api(tags = "车辆管理")
public class VehicleController {

    @Autowired
    private VehicleMapper vehicleMapper;
    //添加车辆
    @PostMapping("add")
    @ApiOperation("添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session){
        //要判断是否登录了
        log.debug("vehicleAddParam:"+ vehicleAddParam);
      UserRegQuery userRegQuery = (UserRegQuery) session.getAttribute("user");
      //登录状态已经在拦截器中进行了校验
//        if (userRegQuery==null){//检验登录状态
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        //有了该用户，该用户添加车辆是，就需要设置该用户的ID
        vehicle.setUserId(userRegQuery.getId());//从登录的用户中去获取ID
//        log.debug("vehicle.setId"+userRegQuery.getId());
        log.debug(vehicle.toString());
        int i = vehicleMapper.add(vehicle);
        return JsonResult.ok(vehicle);
    }
    //车辆列表查询
    @GetMapping("list")
    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(UserListQuery userListQuery,@ApiIgnore HttpSession session){
        log.debug("userListQuery= "+ userListQuery);
        //查询前要查看是否登录
        UserRegQuery userRegQuery = (UserRegQuery)session.getAttribute("user");
//        if (userRegQuery==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        String license = userListQuery.getLicense();
        String brand = userListQuery.getBrand();
        Long userId = userRegQuery.getId();
        //登录状态下就可以进行查询了，查询有三个条件，自己ID名下，然后是品牌和车牌
        List<VehicleListVO> vehicleListVO = vehicleMapper.selectVehicle(brand, license, userId);
        return JsonResult.ok(vehicleListVO);
    }
    //删除车辆
    @PostMapping("del/{id}")
    @ApiOperation("删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehicle(@Range(min = 200,max = 300,message = "车辆编号必须在200-300之间") @PathVariable Integer id, @ApiIgnore HttpSession session) {
        //手动抛出一个异常
        if (id<0){
            throw  new IllegalArgumentException("车辆编号不能小于0");
        }
        log.debug("id="+id);
        UserRegQuery userRegQuery = (UserRegQuery) session.getAttribute("user");
//        if (userRegQuery==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        //登录状态
        vehicleMapper.deleteVehicle(id,userRegQuery.getId());
        return JsonResult.ok();
    }
}



























